package com.nus.fellowmark.modules.user.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.nus.fellowmark.common.response.ResponseEnum;
import com.nus.fellowmark.common.response.ResponseWrapper;
import com.nus.fellowmark.common.response.ServerResponseEntity;
import com.nus.fellowmark.common.utils.PageUtils;
import com.nus.fellowmark.dao.UserDao;
import com.nus.fellowmark.modules.rbac.enums.RoleEnum;
import com.nus.fellowmark.modules.rbac.service.SysUserRoleMappingService;
import com.nus.fellowmark.modules.user.dto.UserLoginDTO;
import com.nus.fellowmark.modules.user.dto.UserRegisterDTO;
import com.nus.fellowmark.modules.user.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 
 *
 * @author Chen Ruoyi
 * @email chenruoyisz@gmail.com
 * @date 2023-02-10
 */
@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private SysUserRoleMappingService sysUserRoleMappingService;

    /**
     * USER LIST
     * */
    @SaCheckRole(value = {"admin", "super-admin"}, mode = SaMode.OR)
    @ApiOperation("List users")
    @RequestMapping("/list")
    public ServerResponseEntity list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);
        return ServerResponseEntity.success(page);
    }

    /**
     * REGISTER
     * */
    @ApiOperation("user register")
    @RequestMapping("/register")
    public ServerResponseEntity register(@RequestBody UserRegisterDTO userRegisterDTO){
        ResponseWrapper<Integer> result = userService.register(userRegisterDTO);
        if (!result.getResponseEnum().value().equals(ResponseEnum.OK.value())) {
            return ServerResponseEntity.fail(result.getResponseEnum());
        }

        return ServerResponseEntity.success();
    }

    /**
     * LOGIN
     * */
    @ApiOperation("user login")
    @RequestMapping("/login")
    public ServerResponseEntity login(@RequestBody UserLoginDTO userLoginDTO) {
        ResponseWrapper<Integer> result = userService.checkPassword(userLoginDTO);
        if (!result.getResponseEnum().value().equals(ResponseEnum.OK.value())) {
            return ServerResponseEntity.showFailMsg(result.getResponseEnum().getMsg());
        }
        StpUtil.login(result.getData());
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo(); // return satoken
        List<String> userRole = StpUtil.getRoleList(); // return user role
        return ServerResponseEntity.success(userRole.get(0), tokenInfo);
    }

    /**
     * HOME PAGES
     * */
    @SaCheckRole(value = {"stu"}, mode = SaMode.OR)
    @ApiOperation("student homepage after login")
    @RequestMapping("/student/home")
    public ServerResponseEntity studentHome(){
        return ServerResponseEntity.success();
    }

    @SaCheckRole(value = {"staff"}, mode = SaMode.OR)
    @ApiOperation("staff homepage after login")
    @RequestMapping("/staff/home")
    public ServerResponseEntity staffHome(){
        return ServerResponseEntity.success();
    }

    @SaCheckRole(value = {"admin", "super-admin"}, mode = SaMode.OR)
    @ApiOperation("admin homepage after login")
    @RequestMapping("/admin/home")
    public ServerResponseEntity adminHome(){
        return ServerResponseEntity.success();
    }

    /**
     * MODULE PAGES
     * */
    @SaCheckRole(value = {"stu"}, mode = SaMode.OR)
    @ApiOperation("student module page after login")
    @RequestMapping("/student/module")
    public ServerResponseEntity studentModule(){
        return ServerResponseEntity.success();
    }

    @SaCheckRole(value = {"staff"}, mode = SaMode.OR)
    @ApiOperation("staff module page after login")
    @RequestMapping("/staff/module")
    public ServerResponseEntity staffModule(){
        return ServerResponseEntity.success();
    }

    @SaCheckRole(value = {"admin", "super-admin"}, mode = SaMode.OR)
    @ApiOperation("admin module page after login")
    @RequestMapping("/admin/module")
    public ServerResponseEntity adminModule(){
        return ServerResponseEntity.success();
    }

    /**
     * LOGOUT
     * */
    @ApiOperation("user logout")
    @RequestMapping("/logout")
    public ServerResponseEntity logout() {
        StpUtil.logout();
        return ServerResponseEntity.success();
    }

    /**
     * USER ADMINISTRATOR SETTINGS
     * */
    @SaCheckRole(value = {"admin", "super-admin"}, mode = SaMode.OR)
    @ApiOperation("user administrator page after login")
    @RequestMapping("/administrator")
    public ServerResponseEntity administratorModule(){
        return ServerResponseEntity.success();
    }

    @SaCheckRole(value = {"admin", "super-admin", "staff"}, mode = SaMode.OR)
    @ApiOperation("delete user")
    @RequestMapping("/delete")
    public ServerResponseEntity delete(@RequestBody Integer[] ids){
        userService.removeByUserIds(Arrays.asList(ids));

        return ServerResponseEntity.success();
    }

}
